package com.lagou.controller;

import com.lagou.domain.*;
import com.lagou.service.MenuService;
import com.lagou.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 李树飞
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    /**
     * 查询角色列表(条件)
     * @param role
     * @return
     */
    @RequestMapping("/findAllRole")
    public ResponseResult findAllRole(@RequestBody Role role){
        List<Role> list = roleService.findAllRole(role);
        return new ResponseResult(true,200,"角色查询成功",list);
    }

    /**
     * 查询所有父子菜单信息
     * @return
     */
    @RequestMapping("/findAllMenu")
    public ResponseResult findSubMenuListByPid() {
        List<Menu> list = menuService.findSubMenuListByPid(-1);

        Map<String, Object> map = new HashMap<>();
        map.put("parentMenuList", list);
        return new ResponseResult(true,200,"查找成功",map);
    }

    /**
     * 根据角色ID查询关联菜单ID
     * @param roleId
     * @return
     */
    @RequestMapping("/findMenuByRoleId")
    public ResponseResult findMenuByRoleId(Integer roleId) {
        List<Integer> list = roleService.findMenuByRoleId(roleId);
        return new ResponseResult(true,200,"查找成功",list);
    }

    /**
     * 为角色分配菜单
     * @param roleMenuVo
     */
    @RequestMapping("/RoleContextMenu")
    public ResponseResult roleContextMenu (@RequestBody RoleMenuVo roleMenuVo) {
        roleService.roleContextMenu(roleMenuVo);
        return new ResponseResult(true,200,"添加成功",null);
    }

    @RequestMapping("/deleteRole")
    public ResponseResult deleteRole(Integer id) {
        roleService.deleteRole(id);
        return new ResponseResult(true,200,"删除成功",null);
    }

    /**
     * 获取当前角色拥有的资源分类和资源信息
     * @param roleId
     * @return
     */
    @RequestMapping("/findResourceListByRoleId")
    public ResponseResult findResourceListByRoleId(Integer roleId) {
        List<ResourceCategory> list = roleService.findResourceListByRoleId(roleId);
        return new ResponseResult(true,200,"查询成功",list);
    }

    /**
     * 为角色分配资源
     * @param roleResourceVo
     * @return
     */
    @RequestMapping("/roleContextResource")
    public ResponseResult roleContextResource(@RequestBody RoleResourceVo roleResourceVo) {
        roleService.roleContextResource(roleResourceVo);
        return new ResponseResult(true,200,"添加成功",null);
    }
}
